<script setup>
import { useRouter } from "vue-router"
import { useRouteStore } from "@/store/route"
import { getRouteList } from "@/api/route"
import { login } from "@/api/user"
import logo from "@/assets/logo.svg"
import { ref } from "vue"

const routeStore = useRouteStore()
const router = useRouter()

const userInfo = ref(
  localStorage.getItem("isRemember") === "true"
    ? JSON.parse(localStorage.getItem("userInfo"))
    : {}
)
const rules = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 2, max: 10, message: "长度在 2 到 10 个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
  ],
}

const loginFormRef = ref(null)
const isRemember = ref(localStorage.getItem("isRemember") === "true")

// 登录
const onLogin = async (formEl) => {
  formEl.validate(async (valid) => {
    if (!valid) return
    const res = await login()
    if (res.code !== 200) return
    const routeList = await getRouteList()
    routeStore.updateRouteList(routeList)
    localStorage.setItem("token", res.data.token)
    routeStore.setActiveMenu(routeList[0])
    localStorage.setItem("userInfo", JSON.stringify(userInfo.value))
    localStorage.setItem("isRemember", isRemember.value)
    router.push("/")
  })
}

const forgetPassword = () => {
  ElMessage.info("请联系管理员")
}
</script>

<template>
  <div class="container">
    <div class="header">
      <img :src="logo" alt="" />
      <h1>Vue-ElementPlus-Admin</h1>
    </div>
    <el-card class="login-box" shodaw="never" shadow="never">
      <div class="logo-box">
        <el-image :src="logo" fit="fit" />
      </div>
      <div class="title">登录 - Vue-ElementPlus-Admin</div>
      <el-form
        ref="loginFormRef"
        class="login-form"
        label-width="auto"
        :rules="rules"
        :model="userInfo">
        <el-form-item prop="username">
          <el-input
            @keyup.enter="onLogin(loginFormRef)"
            placeholder="请输入用户名"
            v-model="userInfo.username"
            :prefix-icon="Avatar">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            @keyup.enter="onLogin(loginFormRef)"
            placeholder="请输入密码"
            v-model="userInfo.password"
            :show-password="true"
            :prefix-icon="Lock">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="isRemember" label="记住我" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onLogin(loginFormRef)">
            登录
          </el-button>
        </el-form-item>
      </el-form>
      <div class="tag">
        <el-link @click="forgetPassword"> 已有账号，忘记密码？ </el-link>
      </div>
    </el-card>
    <div class="footer">
      <el-link :underline="false" href="https://micefind.com" target="_blank">
        © 2025 MiceFind.com
      </el-link>
      <el-link
        href="https://beian.miit.gov.cn/"
        target="_blank"
        :underline="false">
        渝ICP备2022007989号
      </el-link>
    </div>
  </div>
</template>

<style scoped lang="scss">
.container {
  height: 100%;
  position: relative;

  .header {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 50px;
    display: flex;
    align-items: center;
    padding: 0 10px;
    border-bottom: 1px solid var(--el-border-color);
    // box-shadow: var(--el-box-shadow-light);
    img {
      width: 40px;
      margin-right: 10px;
    }
    h1 {
      font-size: 18px;
    }
  }

  .login-box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    // width: 400px;
    width: 40%;
    padding: 35px 0px;
    border-radius: 5px;
    @media (max-width: 768px) {
      width: 100%;
      border: none;
      background-color: transparent;
    }

    .logo-box {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: center;

      .el-image {
        width: 80px;
        height: 80px;
        border-radius: 5px;
      }
    }

    .title {
      width: 100%;
      padding: 30px 0;
      // color: #666;
      font-weight: 700;
      font-size: 18px;
      text-align: center;
    }

    .login-form {
      width: 100%;
      padding: 20px 0 20px 0;

      .el-button {
        width: 100%;
      }
    }

    .tag {
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 0 0 10px 0;

      .el-link {
        padding: 0 10px;
      }
    }

    .other-box {
      display: flex;
      align-items: center;

      .el-link {
        padding: 5px 5px;
      }
    }
  }

  .footer {
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    .el-link {
      padding: 0 5px;
    }
  }
}
</style>
